package srm512;

import java.util.*;

public class DoubleRoshambo {
    public int maxScore(String[] A, String[] E) {
        HashSet<Integer> set = new HashSet<Integer>();
        Map<Integer, List<Integer>> ones = new HashMap<Integer, List<Integer>>();
        HashSet<Integer> done = new HashSet<Integer>();

        int maxScore = 0;
        for (int i = 0; i < A.length; i++) {
            ones.put(i, new ArrayList<Integer>());
            int p;
            for (int j = 0; j < A.length; j++) {
                if ((p = getScore(A[i], E[j])) == 2) {
                    if (!set.contains(j)) {
                        done.add(i);
                        set.add(j);
                        maxScore += 2;
                        break;
                    }
                }
                if (p == 1)
                    ones.get(i).add(j);
            }
        }

        for (int i = 0; i < A.length; i++) {
            if (done.contains(i)) continue;
            for (Integer one : ones.get(i)) {
                if (!set.contains(one)) {
                    maxScore++;
                    set.add(one);
                    done.add(i);
                    break;
                }
            }
        }
        return maxScore;
    }

    private int getScore(String a, String b) {
        if (wins(a.charAt(0), b.charAt(0)) && wins(a.charAt(1), b.charAt(1))) {
            return 2;
        }
        if (wins(a.charAt(1), b.charAt(1)) && (a.charAt(0) == b.charAt(0)))
            return 1;
        return 0;

        /*for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                if (wins(a.charAt(i), b.charAt(j)) && wins(a.charAt(1 - i), b.charAt(1 - j)))
                    return 2;
            }
        }
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                if (wins(a.charAt(i), b.charAt(j)) && (a.charAt(1 - i) == b.charAt(1 - j)))
                    return 1;
            }
        }
        return 0;*/
    }

    private boolean wins(char a, char b) {
        if (a == 'R' && b == 'S') {
            return true;
        } else if (a == 'S' && b == 'P') {
            return true;
        } else if (a == 'P' && b == 'R') {
            return true;
        } else {
            return false;
        }
    }

    // BEGIN KAWIGIEDIT TESTING
    // Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
    private static boolean KawigiEdit_RunTest(int testNum, String[] p0, String[] p1, boolean hasAnswer, int p2) {
        System.out.print("Test " + testNum + ": [" + "{");
        for (int i = 0; p0.length > i; ++i) {
            if (i > 0) {
                System.out.print(",");
            }
            System.out.print("\"" + p0[i] + "\"");
        }
        System.out.print("}" + "," + "{");
        for (int i = 0; p1.length > i; ++i) {
            if (i > 0) {
                System.out.print(",");
            }
            System.out.print("\"" + p1[i] + "\"");
        }
        System.out.print("}");
        System.out.println("]");
        DoubleRoshambo obj;
        int answer;
        obj = new DoubleRoshambo();
        long startTime = System.currentTimeMillis();
        answer = obj.maxScore(p0, p1);
        long endTime = System.currentTimeMillis();
        boolean res;
        res = true;
        System.out.println("Time: " + (endTime - startTime) / 1000.0 + " seconds");
        if (hasAnswer) {
            System.out.println("Desired answer:");
            System.out.println("\t" + p2);
        }
        System.out.println("Your answer:");
        System.out.println("\t" + answer);
        if (hasAnswer) {
            res = answer == p2;
        }
        if (!res) {
            System.out.println("DOESN'T MATCH!!!!");
        } else if ((endTime - startTime) / 1000.0 >= 2) {
            System.out.println("FAIL the timeout");
            res = false;
        } else if (hasAnswer) {
            System.out.println("Match :-)");
        } else {
            System.out.println("OK, but is it right?");
        }
        System.out.println("");
        return res;
    }

    public static void main(String[] args) {
        boolean all_right;
        all_right = true;

        String[] p0;
        String[] p1;
        int p2;

        // ----- test 0 -----
        p0 = new String[]{"RR"};
        p1 = new String[]{"SS"};
        p2 = 2;
        all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
        // ------------------

        // ----- test 1 -----
        p0 = new String[]{"SR", "PP"};
        p1 = new String[]{"PR", "RS"};
        p2 = 1;
        all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
        // ------------------

        // ----- test 2 -----
        p0 = new String[]{"PP", "PP", "PP", "PP"};
        p1 = new String[]{"RR", "PR", "SS", "SP"};
        p2 = 3;
        all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
        // ------------------

        // ----- test 3 -----
        p0 = new String[]{"PS", "RS", "PP", "SP", "RR", "SS", "SP"};
        p1 = new String[]{"RP", "SP", "RR", "SR", "RS", "SP", "SS"};
        p2 = 10;
        all_right = KawigiEdit_RunTest(3, p0, p1, true, p2) && all_right;
        // ------------------

        if (all_right) {
            System.out.println("You're a stud (at least on the example cases)!");
        } else {
            System.out.println("Some of the test cases had errors.");
        }
    }
    // END KAWIGIEDIT TESTING
}
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
